<?php
// src/Inter/ArticleBundle/DataFixtures/ORM/LoadUserData.php
namespace Inter\ArticleBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Inter\ArticleBundle\Entity\User;

class LoadUserData extends AbstractFixture implements FixtureInterface, ContainerAwareInterface
{
	/**
	 * @var ContainerInterface
	 */
	private $container;
	
	/**
	 * {@inheritDoc}
	 */
	public function setContainer(ContainerInterface $container = null)
	{
		$this->container = $container;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public function load(ObjectManager $manager)
	{
		$user = new User();
		$user->setUserName('admin');
		$user->setEmail('abc@yahoo.com');
		$user->setSalt(md5(uniqid()));
		$user->setRole('ROLE_ADMIN');
		$user->setCreateDate(new \DateTime());
		
		$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
		
		$user->setPassword($encoder->encodePassword('secret', $user->getSalt()));
		
		$manager->persist($user);
		$manager->flush();
		
		$this->addReference('user',$user);
	}
	
	/**
	 * {@inheritDoc} 
	 */
	public function getOrder()
	{
		return 1; // the order in which fixtures will be loaded.
	}
}